package com.hlju.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlju.reggie.bean.OrderDetail;
import com.hlju.reggie.bean.Orders;
import com.hlju.reggie.bean.dto.OrderDto;
import com.hlju.reggie.common.BaseContext;
import com.hlju.reggie.common.CustException;
import com.hlju.reggie.common.R;
import com.hlju.reggie.service.OrderDetailService;
import com.hlju.reggie.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService detailService;


    //分页查询（后台管理）
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize,
                        String number, String beginTime, String endTime) {
        //创建分页对象
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        //创建条件构造器
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        //订单编号
        if (number != null && !number.equals("")) {
            wrapper.eq("number", number);
        }
        //判断时间
        if (beginTime != null && !beginTime.equals("") && endTime != null && !endTime.equals("")) {
            wrapper.between("order_time", beginTime, endTime);
        }
        ordersService.page(pageInfo, wrapper);
        return R.sucess(pageInfo);
    }

    //分页查询（移动端）
    @GetMapping("/userPage")
    public R<Page> userPage(Integer page, Integer pageSize) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.getCurrentId());
        wrapper.orderByDesc("order_time");
        ordersService.page(pageInfo,wrapper);

        //创建订单的dto分页对象
        Page<OrderDto> dtoPage = new Page<>();
        //属性复制
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        //创建dto的集合
        List<OrderDto> dtoList = new ArrayList<>();
        //获取订单集合遍历
        List<Orders> orders = pageInfo.getRecords();
        for (Orders order : orders) {
            //创建dto对象
            OrderDto dto = new OrderDto();
            //属性复制
            BeanUtils.copyProperties(order,dto);
            //根据订单id 查询订单明细表
            QueryWrapper<OrderDetail> detailQueryWrapper = new QueryWrapper<>();
            detailQueryWrapper.eq("order_id",order.getId());
            List<OrderDetail> detailList = detailService.list(detailQueryWrapper);
            //设置给dto
            dto.setOrderDetails(detailList);
            //添加进dto的集合
            dtoList.add(dto);
        }
        //给dto分页对象设置集合数据
        dtoPage.setRecords(dtoList);


        return R.sucess(dtoPage);
    }

    //再来一单
    @PostMapping("/again")
    public R again(@RequestBody Orders orders){
        //调用业务层完成功能
        ordersService.again(orders);
        return R.sucess("再来一单成功");
    }

    //修改状态
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders) {
        return ordersService.updateById(orders) ? R.sucess("修改成功") : R.error("修改失败");

    }

    //提交订单
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) throws CustException {
        //调用业务层添加
        ordersService.saveOrder(orders);
        return R.sucess("添加成功");
    }

}
